var cc__decorate = __decorate;
var cc__awaiter = __awaiter;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var $2gameBefore = require("gameBefore");
var $2constant = require("constant");
var $2GameUser = require("GameUser");
var $2GameEvent = require("GameEvent");
var $2PoolManager = require("PoolManager");
var $2RankMgr = require("RankMgr");
var $2TimeManager = require("TimeManager");
var $2UIManager = require("UIManager");
var $2MiniPlatform = require("MiniPlatform");
var $2Platformconf = require("Platformconf");
var $2BaseUIPopup = require("BaseUIPopup");
var $2RankBar = require("RankBar");
var $2UIVMain = require("UIVMain");
var cc__decorator = cc._decorator;
var ccp_ccclass = cc__decorator.ccclass;
var ccp_property = cc__decorator.property;
var _;
(function (e) {
  e[e.Normal = 0] = "Normal";
  e[e.YJWJ = 1] = "YJWJ";
})(_ || (_ = {}));
var def_UIPrank = /* */function (_p$default) {
  function _ctor() {
    var _this125;
    babel_classCallCheck(this, _ctor);
    _this125 = _callSuper(this, _ctor, arguments);
    _this125.rankbar = null;
    _this125.user = null;
    _this125.title = null;
    _this125.rankarr = ["day", "day_yjwj"];
    _this125.rankarr_week = ["week", "week_yjwj"];
    _this125.rankarr_month = ["month", "month_yjwj"];
    _this125.curr_style = _.Normal;
    _this125.curr_time = "day";
    _this125.rankType = "";
    _this125.barlist = null;
    _this125.datalist = [];
    _this125._tempScoreList = {};
    return _this125;
  }
  babel_inherits(_ctor, _p$default);
  return babel_createClass(_ctor, [{
    key: "onShow",
    value: function onShow() {
      var _this126 = this;
      babel_get(babel_getPrototypeOf(_ctor.prototype), "onShow", this).call(this);
      if (!($2Platformconf.default.PlatformType != $2Platformconf.ePlatformType.WXGame && $2Platformconf.default.PlatformType != $2Platformconf.ePlatformType.TTGame)) {
        $2GameUser.default.getInstance().name && $2GameUser.default.getInstance().icon || this.getUserProfile(function (e) {
          return cc__awaiter(_this126, undefined, undefined, /* */babel_regeneratorRuntime().mark(function _callee7() {
            var t;
            var _e70;
            var _n57;
            return babel_regeneratorRuntime().wrap(function _callee7$(_context8) {
              while (1) {
                switch (_context8.prev = _context8.next) {
                  case 0:
                    t = $2GameUser.default.getInstance().PassLevelNum;
                    if (!e) {
                      _context8.next = 11;
                      break;
                    }
                    _context8.next = 4;
                    return $2RankMgr.RankMgr.instance.getScore($2constant.RANKTYPE.day);
                  case 4:
                    _e70 = _context8.sent;
                    _n57 = _e70.find(function (e) {
                      return e.id == $2GameUser.default.getInstance().id;
                    });
                    if (!(_e70.findIndex(function (e) {
                      return e.id == $2GameUser.default.getInstance().id;
                    }), _n57)) {
                      _context8.next = 8;
                      break;
                    }
                    return _context8.abrupt("return");
                  case 8:
                    this.user.getComponent($2RankBar.default).init($2GameUser.default.getInstance().name, t, -1, $2GameUser.default.getInstance().icon);
                    _context8.next = 12;
                    break;
                  case 11:
                    this.user.getComponent($2RankBar.default).init("我", t, -1, "");
                  case 12:
                  case "end":
                    return _context8.stop();
                }
              }
            }, _callee7, this);
          }));
        });
        this.setRankType("day");
      }
      var e = $2TimeManager.TimeManager.getInstance().get_MonthAndDay();
      console.log("str", e);
      this.title.string = e + "排行";
      this.title.node.children[0].getComponent(cc.Label).string = e + "排行";
      cc.Canvas.instance.node.getComponent($2gameBefore.default).BannerHide();
      null != $2UIManager.UIManager.getInstance().getUI($2UIVMain.default) && $2UIManager.UIManager.getInstance().getUI($2UIVMain.default).hideYXQ();
    }
  }, {
    key: "init_title",
    value: function init_title() {}
  }, {
    key: "btn",
    value: function btn(e) {
      babel_get(babel_getPrototypeOf(_ctor.prototype), "click", this).call(this, e, null);
      this.curr_time = e.node.name;
      var t = "11月27日";
      var n = "day";
      if ("day" == this.curr_time) {
        t = $2TimeManager.TimeManager.getInstance().get_MonthAndDay();
        n = this.rankarr[this.curr_style];
      } else if ("week" == this.curr_time) {
        t = "本周";
        n = this.rankarr_week[this.curr_style];
      } else if ("month" == this.curr_time) {
        t = "本月", n = this.rankarr_month[this.curr_style];
      }
      this.title.string = t + "排行";
      this.title.node.children[0].getComponent(cc.Label).string = t + "排行";
      console.log("rank", n);
      this.setRankType(n);
    }
  }, {
    key: "btn2",
    value: function btn2(e) {
      babel_get(babel_getPrototypeOf(_ctor.prototype), "click", this).call(this, e, null);
      var t = e.node.name;
      this.curr_style = _[t];
      console.log("curr_style", t, this.curr_style);
      var n = "day_yjwj";
      if ("day" == this.curr_time) {
        n = this.curr_style == _.Normal ? "day" : "day_yjwj";
      } else if ("week" == this.curr_time) {
        n = this.curr_style == _.Normal ? "week" : "week_yjwj";
      } else {
        "month" == this.curr_time && (n = this.curr_style == _.Normal ? "month" : "month_yjwj");
      }
      console.log("rank", n);
      this.setRankType(n);
    }
  }, {
    key: "close",
    value: function close() {
      babel_get(babel_getPrototypeOf(_ctor.prototype), "close", this).call(this);
      null != $2UIManager.UIManager.getInstance().getUI($2UIVMain.default) && $2UIManager.UIManager.getInstance().getUI($2UIVMain.default).showYXQ();
    }
  }, {
    key: "setRankType",
    value: function setRankType(e) {
      return cc__awaiter(this, undefined, undefined, /* */babel_regeneratorRuntime().mark(function _callee8() {
        var t;
        var n;
        var o;
        var i;
        return babel_regeneratorRuntime().wrap(function _callee8$(_context9) {
          while (1) {
            switch (_context9.prev = _context9.next) {
              case 0:
                if (!(this.rankType == e)) {
                  _context9.next = 2;
                  break;
                }
                return _context9.abrupt("return");
              case 2:
                this.rankType = e;
                t = $2GameUser.default.getInstance().PassLevelNum;
                _context9.next = 6;
                return $2RankMgr.RankMgr.instance.getScore($2constant.RANKTYPE[e]);
              case 6:
                n = _context9.sent;
                o = n.find(function (e) {
                  return e.id == $2GameUser.default.getInstance().id;
                });
                i = n.findIndex(function (e) {
                  return e.id == $2GameUser.default.getInstance().id;
                });
                if (o) {
                  this.user.getComponent($2RankBar.default).init(o.name, o.data[this.rankType], i, o.icon);
                } else if ($2GameUser.default.getInstance().name && $2GameUser.default.getInstance().icon) {
                  this.user.getComponent($2RankBar.default).init($2GameUser.default.getInstance().name, t, -1, $2GameUser.default.getInstance().icon);
                } else {
                  this.user.getComponent($2RankBar.default).init("我", t, -1, "");
                }
                this.init(n);
              case 9:
              case "end":
                return _context9.stop();
            }
          }
        }, _callee8, this);
      }));
    }
  }, {
    key: "click",
    value: function click(e, t) {
      babel_get(babel_getPrototypeOf(_ctor.prototype), "click", this).call(this, e, t);
      var n = e.getCurrentTarget();
      if ("close" == t) {
        $2Platformconf.default.PlatformType === $2Platformconf.ePlatformType.WXGame && cc.Canvas.instance.node.getComponent($2gameBefore.default).BannerShow();
        this.close();
      } else {
        "share" == n.name && $2MiniPlatform.default.Instance.shareAppMessage(function (e) {
          true === e && console.log("分享成功");
        }, {
          videoTag: $2GameUser.default.getInstance().currlevelName
        });
      }
    }
  }, {
    key: "init",
    value: function init(e) {
      console.log("排行榜数据初始化", e);
      this.datalist = e;
      var t = 0;
      for (; t < this.datalist.length && t < 100; t++) {
        var _e71 = this.datalist[t];
        var _n58 = this.barlist.children[t];
        _n58 || (_n58 = $2PoolManager.default.getInstance().get(this.rankbar));
        _n58.setParent(this.barlist);
        _n58.getComponent($2RankBar.default).init(_e71.name, _e71.data[this.rankType], t, _e71.icon);
      }
      for (; t < this.barlist.children.length;) {
        var _e72 = this.barlist.children[t];
        _e72 && $2PoolManager.default.getInstance().put(_e72);
      }
    }
  }, {
    key: "getUserProfile",
    value: function getUserProfile(e) {
      if ($2Platformconf.default.PlatformType === $2Platformconf.ePlatformType.KWaiGame) {
        console.log("用户进入快手平台：获取排行榜信息");
        return void window.ks.getUserInfo({
          success(t) {
            console.log("玩家已授权游戏", t);
            if (t && t.userInfo) {
              console.log("nickName", t.userInfo.nickName);
              console.log("avatarUrl", t.userInfo.avatarUrl);
              $2GameUser.default.getInstance().name = t.userInfo.nickName;
              $2GameUser.default.getInstance().icon = t.userInfo.avatarUrl;
              $2GameUser.default.getInstance().PassLevelNum > 0 && $2RankMgr.RankMgr.instance.putScore($2constant.RANKTYPE.day, $2GameUser.default.getInstance().PassLevelNum);
              e && e(true);
            }
          },
          fail() {
            $2GameUser.default.getInstance().name = "";
            $2GameUser.default.getInstance().icon = "";
            if (window.ks.getUserProfile) {
              $2GameEvent.default.sendEvent("authorization", "show");
              window.ks.getUserProfile({
                desc: "用于显示排行榜数据",
                success(t) {
                  console.log("小程序授权接口调用成功", t);
                  if (t && t.userInfo) {
                    console.log("nickName", t.userInfo.nickName);
                    console.log("avatarUrl", t.userInfo.avatarUrl);
                    $2GameUser.default.getInstance().name = t.userInfo.nickName;
                    $2GameUser.default.getInstance().icon = t.userInfo.avatarUrl;
                    $2GameUser.default.getInstance().PassLevelNum > 0 && $2RankMgr.RankMgr.instance.putScore($2constant.RANKTYPE.day, $2GameUser.default.getInstance().PassLevelNum);
                    e && e(true);
                  }
                  $2GameEvent.default.sendEvent("authorization", "success");
                },
                fail() {
                  $2GameEvent.default.sendEvent("authorization", "fail");
                  e && e(false);
                }
              });
            }
          },
          complete() {}
        });
      }
      if (cc.sys.platform === cc.sys.WECHAT_GAME) {
        console.log("用户进入微信平台：获取排行榜信息");
        wx.getUserInfo({
          withCredentials: true,
          lang: "zh_CN",
          success(t) {
            console.log("玩家已授权游戏", t);
            if (t && t.userInfo) {
              console.log("nickName", t.userInfo.nickName);
              console.log("avatarUrl", t.userInfo.avatarUrl);
              $2GameUser.default.getInstance().name = t.userInfo.nickName;
              $2GameUser.default.getInstance().icon = t.userInfo.avatarUrl;
              $2GameUser.default.getInstance().PassLevelNum > 0 && $2RankMgr.RankMgr.instance.putScore($2constant.RANKTYPE.day, $2GameUser.default.getInstance().PassLevelNum);
              e && e(true);
            }
          },
          fail() {
            $2GameUser.default.getInstance().name = "";
            $2GameUser.default.getInstance().icon = "";
            if (window.wx.getUserProfile) {
              $2GameEvent.default.sendEvent("authorization", "show");
              window.wx.getUserProfile({
                desc: "用于显示排行榜数据",
                success(t) {
                  console.log("小程序授权接口调用成功", t);
                  if (t && t.userInfo) {
                    console.log("nickName", t.userInfo.nickName);
                    console.log("avatarUrl", t.userInfo.avatarUrl);
                    $2GameUser.default.getInstance().name = t.userInfo.nickName;
                    $2GameUser.default.getInstance().icon = t.userInfo.avatarUrl;
                    $2GameUser.default.getInstance().PassLevelNum > 0 && $2RankMgr.RankMgr.instance.putScore($2constant.RANKTYPE.day, $2GameUser.default.getInstance().PassLevelNum);
                    e && e(true);
                  }
                  $2GameEvent.default.sendEvent("authorization", "success");
                },
                fail() {
                  $2GameEvent.default.sendEvent("authorization", "fail");
                  e && e(false);
                }
              });
            }
          },
          complete() {}
        });
      } else if (cc.sys.platform === cc.sys.BYTEDANCE_GAME) {
        console.log("用户进入抖音平台：获取排行榜信息"), window.tt.getUserInfo && window.tt.getUserInfo({
          withCredentials: true,
          success(t) {
            console.log("getUserInfo 调用成功：", t, t.userInfo);
            if (t && t.userInfo) {
              $2GameUser.default.getInstance().name = t.userInfo.nickName;
              $2GameUser.default.getInstance().icon = t.userInfo.avatarUrl;
              $2GameUser.default.getInstance().PassLevelNum > 0 && $2RankMgr.RankMgr.instance.putScore($2constant.RANKTYPE.day, $2GameUser.default.getInstance().PassLevelNum);
              e && e(true);
            }
          },
          fail() {
            $2GameUser.default.getInstance().name = "";
            $2GameUser.default.getInstance().icon = "";
            $2GameEvent.default.sendEvent("authorization", "show");
            window.tt.getUserProfile && window.tt.getUserProfile({
              success(t) {
                console.log("getUserProfile 调用成功：", t, t.userInfo);
                if (t && t.userInfo) {
                  $2GameUser.default.getInstance().name = t.userInfo.nickName;
                  $2GameUser.default.getInstance().icon = t.userInfo.avatarUrl;
                  $2GameUser.default.getInstance().PassLevelNum > 0 && $2RankMgr.RankMgr.instance.putScore($2constant.RANKTYPE.day, $2GameUser.default.getInstance().PassLevelNum);
                }
                e && e(true);
                $2GameEvent.default.sendEvent("authorization", "success");
              },
              fail() {
                $2GameEvent.default.sendEvent("authorization", "fail");
                e && e(false);
              }
            });
          }
        });
      }
    }
  }, {
    key: "updateScoreList",
    value: function updateScoreList(e, t) {
      null == e && (e = []);
      var n = false;
      for (var _o124 = 0; _o124 < e.length; _o124++) {
        if (null != e[_o124].data) {
          e[_o124].iself = false;
          if (e[_o124].id == $2GameUser.default.getInstance().id) {
            if ($2GameUser.default.getInstance().checkAuthorization()) {
              e.splice(_o124, 1);
              _o124--;
              continue;
            }
            e[_o124].data[$2constant.RANKTYPE[t]] = $2GameUser.default.getInstance()[$2constant.RANKTYPE[t]];
            e[_o124].icon = $2GameUser.default.getInstance().icon;
            e[_o124].name = $2GameUser.default.getInstance().name;
            e[_o124].uid = $2GameUser.default.getInstance().uid;
            n = true;
          }
          e[_o124].type = $2constant.RANKTYPE[t];
        } else {
          e.splice(_o124, 1);
          _o124--;
        }
      }
      if (0 == n && $2GameUser.default.getInstance().checkAuthorization()) {
        var _n59 = {};
        _n59.id = $2GameUser.default.getInstance().id;
        _n59.icon = $2GameUser.default.getInstance().icon;
        _n59.name = $2GameUser.default.getInstance().name;
        _n59.data = {};
        _n59.data[$2constant.RANKTYPE[t]] = $2GameUser.default.getInstance()[$2constant.RANKTYPE[t]];
        _n59.type = $2constant.RANKTYPE[t];
        _n59.uid = $2GameUser.default.getInstance().uid;
        e.push(_n59);
      }
      for (var _o125 = e.length - 1; _o125 >= 0; _o125--) {
        "" == e[_o125].icon && e.splice(_o125, 1);
      }
      e.sort(function (e, n) {
        return n.data[$2constant.RANKTYPE[t]] - e.data[$2constant.RANKTYPE[t]];
      });
      e.length > 100 && e.pop();
      this._tempScoreList[$2constant.RANKTYPE[t]] = e;
    }
  }]);
}($2BaseUIPopup.default);
def_UIPrank.className = "UIPrank";
cc__decorate([ccp_property(cc.Prefab)], def_UIPrank.prototype, "rankbar", undefined);
cc__decorate([ccp_property(cc.Node)], def_UIPrank.prototype, "user", undefined);
cc__decorate([ccp_property(cc.Label)], def_UIPrank.prototype, "title", undefined);
cc__decorate([ccp_property(cc.Node)], def_UIPrank.prototype, "barlist", undefined);
def_UIPrank = cc__decorate([ccp_ccclass], def_UIPrank);
exports.default = def_UIPrank;